package com.maxwell.googleplay.views;

import com.maxwell.googleplay.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.FrameLayout;

/**
 * @author yue
 * @创建时间 2016-4-1下午7:53:27
 * @描述 TODO
 * 
 */
public class RatioLayout extends FrameLayout {

	private float mPicRatio = 0f;// 图片的宽高比
	public void setPicRatio(float picRatio) {
		mPicRatio = picRatio;
	}

	public static final int RELATIVE_WIDTH = 0;//
	public static final int RELATIVE_HEIGHT = 1;
	private int mRelative = RELATIVE_WIDTH;

	public void setRelative(int relative) {
		mRelative = relative;
	}

	public RatioLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout);
		
		mPicRatio = typedArray.getFloat(R.styleable.RatioLayout_picRatio, 0);
		mRelative = typedArray.getInt(R.styleable.RatioLayout_relative, RELATIVE_WIDTH);
		
		typedArray.recycle();
	}

	public RatioLayout(Context context) {
		this(context, null);
	}

	// onMeasure onLayout onDraw
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		int parentWidthMode = MeasureSpec.getMode(widthMeasureSpec);
		int parentHeightMode = MeasureSpec.getMode(heightMeasureSpec);

		if (parentWidthMode == MeasureSpec.EXACTLY && mPicRatio != 0 && mRelative == RELATIVE_WIDTH) {// 宽度固定，已知图片的宽高比，求控件的高度
			// 得到父容器的宽度
			int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
			// 得到孩子的宽度
			int childWidth = parentWidth - getPaddingLeft() - getPaddingRight();
			// 计算孩子的高度
			int childHeight = (int) (childWidth / mPicRatio + 0.5f);
			// 计算父容器的高度
			int parentHeight = childHeight + getPaddingBottom()
					+ getPaddingTop();

			// 主动测绘孩子，固定宽高比
			int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth,
					MeasureSpec.EXACTLY);
			int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
					childHeight, MeasureSpec.EXACTLY);
			;
			measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);

			// 设置自己的测绘结果
			setMeasuredDimension(parentWidth, parentHeight);

		} else if (parentHeightMode == MeasureSpec.EXACTLY && mPicRatio != 0 && mRelative == RELATIVE_HEIGHT) {
			// 得到父容器的高度
			int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
			// 得到孩子的高度
			int childHeight = parentHeight - getPaddingBottom()
					- getPaddingTop();
			// 计算孩子的宽度
			int childWidth = (int) (childHeight * mPicRatio + 0.5f);
			// 计算父容器的宽度
			int parentWidth = childWidth + getPaddingLeft() + getPaddingRight();

			// 主动测绘孩子，固定宽高比
			int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth,
					MeasureSpec.EXACTLY);
			int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
					childHeight, MeasureSpec.EXACTLY);
			;
			measureChildren(childWidthMeasureSpec, childHeightMeasureSpec);

			// 设置自己的测绘结果
			setMeasuredDimension(parentWidth, parentHeight);

		} else {
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		}

	}

}
